﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using System.Reflection;

namespace ui
{
    public static class _functions
    {
        public static void loadFile(string file, List<_control> controls,out _settings set, frmMain parent, PictureBox window)
        {            
            XmlSerializer serial;

            List<_control> cout = new List<_control>();

            foreach (_control c in controls)
            {
                c.pic.Dispose();
            }
            controls.Clear();

            set = new _settings();

            XmlDocument xml = new XmlDocument();
            StreamReader sr = new StreamReader(file);
            xml.Load(new StreamReader(file));

            XmlNode settingsnode = xml.SelectSingleNode("//settings");
            string settingsdata = settingsnode.FirstChild.InnerText;

            serial = new XmlSerializer(set.GetType());

            TextReader reader = new StringReader(settingsdata);
            set = (_settings)serial.Deserialize(reader);

            XmlNodeList list = xml.SelectNodes("//file");

            foreach (XmlNode node in list)
            {
                string xmldata = node.FirstChild.InnerText;
                string xmltype = node.Attributes[0].Value;

                serial = new XmlSerializer(Type.GetType(xmltype));

                reader = new StringReader(xmldata);

                _control c = (_control)serial.Deserialize(reader);

                c.parent = parent;
                c.setup(window);
                c.pic.MouseDown += new MouseEventHandler(parent.pic_MouseDown);

                c.pic.Left = c.x;
                c.pic.Top = c.y;
                c.pic.Width = c.width;
                c.pic.Height = c.height;

                controls.Add(c);
            }

            parent.refresh();
            sr.Close();
        }

        public static void saveFile(string file, List<_control> controls, _settings set)
        {
            TextWriter writer = new StreamWriter(file);

            writer.Write(generateXML(controls,set));

            writer.Close();
        }

        public static string generateXML(List<_control> controls,_settings set)
        {
            XmlSerializer serial;
            TextWriter writer = new StringWriter();

            writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-16\"?>");
            writer.WriteLine("<root>");

            writer.WriteLine("<settings>");
            writer.Write("<![CDATA[");
            serial = new XmlSerializer(set.GetType());
            serial.Serialize(writer, set);
            writer.Write("]]>");
            writer.WriteLine("</settings>");

            writer.WriteLine("<uifiles>");

            foreach (_control c in controls)
            {
                writer.WriteLine("<file type=\"" + c.GetType().ToString() + "\">");
                writer.Write("<![CDATA[");
                serial = new XmlSerializer(c.GetType());
                serial.Serialize(writer, c);
                writer.Write("]]>");
                writer.WriteLine("</file>");
            }

            writer.WriteLine("</uifiles>");
            writer.WriteLine("</root>");

            writer.Close();

            return writer.ToString();
        }

        public static string generateCode(List<_control> controls,int cW, int cH)
        {
            StringBuilder d = new StringBuilder();
            StringBuilder i = new StringBuilder();

            _window win = null;

            foreach (_control c in controls)
            {
                switch (c.type)
                {
                    case ControlTypes.Window:
                        win = (_window)c;
                        break;
                    default:
                        d.Append("\tUI." + c.type.ToString() + " ");
                        d.Append(c.name);
                        d.Append(";");
                        d.Append("\r\n");
                        break;
                }
            }

            if (win == null) return null;

            i.AppendLine("namespace codebase");
            i.AppendLine("{");

            i.Append("public class ");
            i.Append(win.name);
            i.Append(" : UI.Window");
            i.AppendLine();
            i.AppendLine("{");

            i.Append(d.ToString());
            i.AppendLine();

            i.AppendLine("\tpublic " + win.name + "(UI.WindowManager win)");
            i.AppendLine("\t{");
            i.AppendLine("\t\tInitialize(win);");
            i.AppendLine("\t}");

            i.AppendLine();

            i.AppendLine("\tpublic void Initialize(UI.WindowManager man)");
            i.AppendLine("\t{");
            i.AppendLine("\t\tbase.Initialize(man,0,0);");

            if (win.Caption != "") { i.AppendLine("\t\tText = \"" + win.Caption + "\";"); i.AppendLine(); }
            if (win.Visible) i.AppendLine("\t\tVisible = true;");
            if (win.FullScreen) i.AppendLine("\t\tFullScreen = true;");
            i.AppendLine("\t\tWidth = " + win.width + ";");
            i.AppendLine("\t\tHeight = "+ win.height + ";");
            if (win.DrawBorder) i.AppendLine("\t\tDrawBorder = true;");
            if (win.Gradient) i.AppendLine("\t\tGradient = true;");
            if (win.Gradient && win.GradientMode != _window.GradientModeEnum.Blue) i.AppendLine("\t\tGradientMode = GradientType." + win.GradientMode.ToString() + ";");
            if (win.Effect) i.AppendLine("\t\tEffect = true;");
            if (win.Effect && win.EffectMode == _window.EffectModeEnum.Squares) i.AppendLine("\t\tEffectMode = EffectType." + win.EffectMode.ToString() + ";");
            if (win.Effect && win.EffectColor != Color.White) i.AppendLine("\t\tEffectColor = ColorExtensions.RGB16(" + win.EffectColor.R + "," + win.EffectColor.G + "," + win.EffectColor.B + ");");
            if (win.BorderColor != Color.Black) i.AppendLine("\t\tBorderColor = ColorExtensions.RGB16(" + win.BorderColor.R + "," + win.BorderColor.G + "," + win.BorderColor.B + ");");
            if (win.BackgroundColor != Color.White) i.AppendLine("\t\tBackgroundColor = ColorExtensions.RGB16(" + win.BackgroundColor.R + "," + win.BackgroundColor.G + "," + win.BackgroundColor.B + ");");
            if (win.CaptionColor != Color.Black) i.AppendLine("\t\tCaptionColor = ColorExtensions.RGB16(" + win.CaptionColor.R + "," + win.CaptionColor.G + "," + win.CaptionColor.B + ");");
            if (win.ForeColor != Color.White) i.AppendLine("\t\tForeColor = ColorExtensions.RGB16(" + win.ForeColor.R + "," + win.ForeColor.G + "," + win.ForeColor.B + ");");

            if (controls.Count > 1) i.AppendLine();

            int cnt = 0;
            bool skipCRLF = false;

            foreach (_control c in controls)
            {
                cnt++;
                skipCRLF = false;

                switch (c.type)
                {
                    case ControlTypes.Button:

                        _button b = (_button)c;
                        i.AppendLine("\t\t" + b.name + " = (UI.Button)Add(UI.Window.ControlTypes.Button," + b.x / cW + "," + b.y / cH + ");");
                        i.AppendLine("\t\t" + b.name + ".Text = \"" + b.text + "\";");
                        i.AppendLine("\t\t" + b.name + ".isStateButton = " + b.StateButton.ToString().ToLower() + ";");
                        i.AppendLine("\t\t" + b.name + ".Width = " + b.width + ";");
                        i.AppendLine("\t\t" + b.name + ".Height = " + b.height + ";");

                        if (b.BackgroundColor != Color.White) i.AppendLine("\t\t" + b.name + ".BackgroundColor = ColorExtensions.RGB16(" + b.BackgroundColor.R + "," + b.BackgroundColor.G + "," + b.BackgroundColor.B + ");");
                        if (b.BorderColor != Color.Black) i.AppendLine("\t\t" + b.name + ".BorderColor = ColorExtensions.RGB16(" + b.BorderColor.R + "," + b.BorderColor.G + "," + b.BorderColor.B + ");");
                        if (b.NotPressedColor != Color.Red) i.AppendLine("\t\t" + b.name + ".NotPressedColor = ColorExtensions.RGB16(" + b.NotPressedColor.R + "," + b.NotPressedColor.G + "," + b.NotPressedColor.B + ");");
                        if (b.PressedColor != Color.Green) i.AppendLine("\t\t" + b.name + ".PressedColor = ColorExtensions.RGB16(" + b.PressedColor.R + "," + b.PressedColor.G + "," + b.PressedColor.B + ");");
                        if (b.ForeColor != Color.Black) i.AppendLine("\t\t" + b.name + ".ForeColor = ColorExtensions.RGB16(" + b.ForeColor.R + "," + b.ForeColor.G + "," + b.ForeColor.B + ");");
                        if (b.Padding != 1) i.AppendLine("\t\t" + b.name + ".Padding = " + b.Padding + ";");

                        break;

                    case ControlTypes.Frame:

                        _frame f = (_frame)c;
                        i.AppendLine("\t\t" + f.name + " = (UI.Frame)Add(UI.Window.ControlTypes.Frame," + f.x / cW + "," + f.y / cH + ");");
                        if (f.Caption != "") i.AppendLine("\t\t" + f.name + ".Text = \"" + f.Caption + "\";");
                        i.AppendLine("\t\t" + f.name + ".Width = " + f.width + ";");
                        i.AppendLine("\t\t" + f.name + ".Height = " + f.height + ";");

                        if (f.BorderColor != Color.Black) i.AppendLine("\t\t" + f.name + ".BorderColor = ColorExtensions.RGB16(" + f.BorderColor.R + "," + f.BorderColor.G + "," + f.BorderColor.B + ");");
                        if (f.BackgroundColor != Color.White) i.AppendLine("\t\t" + f.name + ".BackgroundColor = ColorExtensions.RGB16(" + f.BackgroundColor.R + "," + f.BackgroundColor.G + "," + f.BackgroundColor.B + ");");

                        break;

                    case ControlTypes.Window:
                        skipCRLF = true; ;
                        break;
                }

                if (cnt < controls.Count && !skipCRLF) i.AppendLine();

            }

            i.AppendLine("\t}");
            i.AppendLine("}");
            i.Append("}");

            return i.ToString();
        }
    }
}
